/***************************************************************************
    begin                : jan 01 2013
    copyright            : (C) 2013 by Benoit Valot
    email                : benvalot@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 23 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

package val.lemonde;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class FragmentMain extends FragmentLemondeBase implements OnItemClickListener {
    private OnCategorieSelectedListener listener;
	private ListView maListViewPerso;
	private ArrayList<Categorie> categories;

    public interface OnCategorieSelectedListener {
        public void onCategorieSelected(Categorie cat, int pos);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
        	this.listener = (OnCategorieSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnCategorieSelectedListener");
        }
    }
    
	@Override
	public void onResume() {
		categories = this.get_datas().get_categorie(false);
		Parcelable state = maListViewPerso.onSaveInstanceState();
		this.load_content();
		maListViewPerso.onRestoreInstanceState(state);
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d("lemonde", "OnCreateView Main");
		View view = inflater.inflate(R.layout.list_view,
                container, false);
//		categories = this.get_datas().get_categorie(false);
		maListViewPerso = (ListView) view.findViewById(R.id.listviewperso);
//		this.load_content();
		return(view);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {				
		@SuppressWarnings("unchecked")
		HashMap<String, String> map = (HashMap<String, String>) maListViewPerso.getItemAtPosition(arg2);
		listener.onCategorieSelected(categories.get(Integer.parseInt(map.get("int"))),arg2);
	}

	public void load_content() {
		// Création de la ArrayList qui nous permettra de remplire la listView
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;

		for (Categorie cat : categories) {
			if (cat.isVisible()) {
				map = new HashMap<String, String>();
				map.put("titre", cat.getName());
				map.put("int", ""+cat.getPosition());
				listItem.add(map);
			}
		}

		// Création d'un SimpleAdapter qui se chargera de mettre les items
		// présent dans notre list (listItem) dans la vue affichageitem
		SimpleAdapter mSchedule = new SimpleAdapter(this.getActivity().getApplicationContext(),
				listItem, R.layout.categorie_listes, new String[] { "titre" },
				new int[] { R.id.cat_title });

		// On attribut à notre listView l'adapter que l'on vient de créer
		maListViewPerso.setAdapter(mSchedule);

		// Enfin on met un écouteur d'évènement sur notre listView
		maListViewPerso.setOnItemClickListener(this); 
	}
	
	public ListView getListView(){
		return maListViewPerso;
	}
	
}
